package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.mapper.PermissionsMapper;
import com.li.mapper.RoleMapper;
import com.li.mapper.UserMapper;
import com.li.pojo.Permission;
import com.li.pojo.Role;
import com.li.pojo.User;
import com.li.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionsMapper permissionsMapper;
    public User findByUsername(String username) {

        //获取基本的用户信息
        User user = userMapper.findByUsername(username);
        if(user==null){
            return null;
        }
        //获取用户所拥有的角色信息
        Set<Role> roles = roleMapper.findByUserId(user.getId());

        if(roles.size()>0&&roles!=null){

            for (Role role : roles) {

                Integer roleId = role.getId();
                Set<Permission> permissions = permissionsMapper.findByRoleId(roleId);
                if(permissions!=null&&permissions.size()>0){
                    role.setPermissions(permissions);
                }
            }
        }
        user.setRoles(roles);

        return user;
    }

    @Override
    public void add(User user, Integer[] roleIds) {

        //新增用户
        userMapper.add(user);
        //新增用户所关联的角色
        for (Integer roleId : roleIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("roleId",roleId);
            map.put("userId",user.getId());
            userMapper.addUserAndRole(map);
        }
    }

    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //执行查询
        Page<User> list=userMapper.findByCondition(queryPageBean.getQueryString());
        //封装结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(list.getTotal());
        return result;
    }

    @Override
    public void delete(Integer userId) {
        //通过id删除用户
        //删除用户所关联的角色
        userMapper.deleteUserAndRole(userId);
        //删除用户
        userMapper.delete(userId);
    }

    @Override
    public List<Integer> findRoleByUserId(Integer userId) {
        //通过会员id查询所关联的角色ids
        return userMapper.findRoleIdsByUserId(userId);
    }

    @Override
    public User get(Integer userId) {
        return userMapper.findById(userId);
    }

    @Override
    public void update(User user, Integer[] roleIds) {
        //删除用户对应的角色
        userMapper.deleteUserAndRole(user.getId());
        //更新用户
        userMapper.update(user);
        //新增关联
        for (Integer roleId : roleIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("userId",user.getId());
            map.put("roleId",roleId);
            userMapper.addUserAndRole(map);
        }
    }
}
